﻿using SoundEngine3;
using System;

namespace DefaultDspPluginsLib.Depricated
{
    public static class Common_Modulizer_LFO
    {
        public static void Process(
            int length, int waveformBufferLength, ref double currentSample, double sync, double sync_position, double tempo
            , double[] frequency, double[] amplitude, double[] amplitudeOffset
            , double[] waveForm, double[] output
            , double sampleRate)
        {
            sampleRate = 1.0 / sampleRate;

            if (waveForm != null)
            {
                // Sync off.
                if (sync == 0)
                {
                    for (int i = 0; i < length; i++)
                    {
                        uint index = (uint)(++currentSample * frequency[i] * sampleRate * waveformBufferLength);
                        output[i] = waveForm[index % waveformBufferLength] * amplitude[i] + amplitudeOffset[i];
                    }
                }
                else
                {
                    // Sync current sample.
                    currentSample = sync_position;

                    double syncValue = (double)sync * 0.001;
                    double syncFreq = (tempo / 60) * sampleRate / syncValue;
                    for (int i = 0; i < length; i++)
                    {
                        uint index = (uint)(++currentSample * syncFreq * waveformBufferLength);
                        output[i] = waveForm[index % waveformBufferLength] * amplitude[i] + amplitudeOffset[i];
                    }
                }
            }
            else
            {
                // Sinus.

                // Sync off.
                if (sync == 0)
                {
                    for (int i = 0; i < length; i++)
                    {
                        output[i] = Math.Sin((2 * Math.PI * ++currentSample * frequency[i]) * sampleRate) * amplitude[i] + amplitudeOffset[i];
                    }
                }
                else
                {
                    // Sync current sample.
                    currentSample = sync_position;

                    double syncValue = (double)sync * 0.001;
                    double syncFreq = (tempo / 60) * sampleRate / syncValue;
                    for (int i = 0; i < length; i++)
                    {
                        output[i] = Math.Sin((2 * Math.PI * ++currentSample) * syncFreq) * amplitude[i] + amplitudeOffset[i];
                    }
                }
            }
        }
    }
}
